<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright"  />
<title></title>
<link rel="stylesheet" type="text/css" href="../easyui/1.5.3/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../css/wu.css" />
<link rel="stylesheet" type="text/css" href="../css/icon.css" />
<script type="text/javascript" src="../js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="../js/jquery.serializejson.min.js"></script>
<script type="text/javascript" src="../easyui/1.5.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../easyui/1.5.3/locale/easyui-lang-zh_CN.js"></script>
<!--<script type="text/javascript" src="../js/xheditor/jquery/jquery-1.4.4.min.js"></script>-->
<script type="text/javascript" src="../js/xheditor/xheditor-1.2.2.min.js"></script>
<script type="text/javascript" src="../js/xheditor/xheditor_lang/zh-cn.js"></script>
</head>
<body>
	<div class="easyui-layout" data-options="fit:true">
		<div data-options="region:'west',split:true" title:"west" style="width:155px;">
			<ul id="tt"></ul>
			
		</div>
		<div data-options="region:'center',title:'News Manager',iconCls:'icon-ok'" style="height:auto">
			<div id="user-toolbar">
			    <div class="wu-toolbar-button">
			        <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openNewsAdd()" plain="true">添加</a>
			        <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="" plain="true">修改</a>
			        <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="removeNews()" plain="true">删除</a>
			        <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="reload()" plain="true">刷新</a>
			        
			    </div>
			    <div class="wu-toolbar-search">
			        <label>起始时间：</label><input class="easyui-datebox" style="width:100px">
			        <label>结束时间：</label><input class="easyui-datebox" style="width:100px">
			        <label>关键词：</label><input class="wu-text" style="width:100px">
			        <a href="#" class="easyui-linkbutton" iconCls="icon-search">开始检索</a>
			    </div>
			</div>
			
			<table id="dg"></table>
			
			
			
			<div id="news-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:80%; padding:10px;">
				 <form id="news-form" class="easyui-form" method="post" data-options="novalidate:true">
				    <div style="margin-bottom:20px">
				       	<input class="easyui-textbox name" id="title" name="title" style="width:100%" data-options="label:'标题:',required:true">
				    </div>
				    <div style="margin-bottom:20px">
				       	<input class="easyui-textbox" name="author" id="author" style="width:100%" data-options="label:'作者:',required:true">
				    </div>
				 
				    <div style="margin-bottom:20px">
				      	<input class="easyui-textbox" name="btitle" id="btitle" style="width:100%" data-options="label:'副标题:'">
				    </div>
				    <div style="margin-bottom:20px">
				       	<textarea class="xheditor" name="content" id="content" style="width:100%;height:150px" data-options="required:true"></textarea>
				    </div>
				    <div style="margin-bottom:20px;display:none">
				       	<input class="easyui-textbox" name="cateId" id="cateId" value="59cb6465911af53334b931a8" style="width:100%" data-options="label:'cateId',required:true">
				    </div>
				</form>
			</div>
			
		</div>
		
	</div>
	
    <script type="text/javascript">
    	var globalId = null;
    	$(pageInit);
        function pageInit()
        {
            //$.extend(XHEDITOR.settings,{shortcuts:{'ctrl+enter':submitForm}});
            $('#content').xheditor({
                html5Upload:false,
                upMultiple:1,

                upLinkUrl:"upload.php",
                upLinkExt:"zip,rar,txt",

                upImgUrl:"/comment/upLoadImg",
                upImgExt:"jpg,jpeg,gif,png",

                upFlashUrl:"upload.php",
                upFlashExt:"swf",

                upMediaUrl:"upload.php",
                upMediaExt:"wmv,avi,wma,mp3,mid"
            });
           
        }
    	
    	
    	var nodeId = null;
		$('#tt').tree({
			animate:true,
		    url:'http://localhost:3000/news/list',
		    method:'get',
			onClick: function(node){
				console.log(node._id);
				$('#cateId').textbox('setValue',node._id);
				initList(node._id);
			}

		});
		
		function initList(cateId){
    		var postUrl;
    		if(cateId!=undefined && cateId.trim().length>0){
    			queryParams = {cateId:cateId}
    		}else{
    			queryParams = {}
    		}
    		
    		$('#dg').datagrid({
			    url:'http://localhost:3000/newsgl/list',
			    method:"post",
			    fit:true,
			    queryParams:queryParams,
			    pageSize:4,
			    pageList:[2,4,6,8,10],
			    pagination:true,
			    toolbar:'#user-toolbar',
			    columns:[[
			    	{ checkbox:true,field:'columns'},
			        {field:'title',title:'标题',width:130},
			        {field:'author',title:'作者',width:50},
			        {field:'btitle',title:'副标题',width:150},
			        {field:'content',title:'内容',width:300},
			        {field:'date',title:'发布时间',width:150},
			        {field:'category',title:'分类',width:50},
			        {field:'operate',title:'管理', width:170,
						formatter: function(value,row,index){
							return '<a  href="javascript:void(0)"  onclick="openNewsEdit(\''+ row._id+'\')" class="easyui-linkbutton change">修改</a> '+
							'<a href="javascript:void(0)" onclick="deleNews(\''+ row._id+'\')" class="easyui-linkbutton del">删除</a>'+
							'<a href="javascript:void(0)" onclick="add(\''+ row._id+'\')" class="easyui-linkbutton check">查看</a>'
						}
					}
			    ]],
			    onLoadSuccess:function(data){  
			        $('.change').linkbutton({text:'修改',plain:true,iconCls:'icon-edit'}); 
			        $('.del').linkbutton({text:'删除',plain:true,iconCls:'icon-edit'});
			        $('.check').linkbutton({text:'查看',plain:true,iconCls:'icon-edit'});
			    }  
			});
			
			
    	}
    	initList('59cb6465911af53334b931a8');
    	//查看
    	/**
		* Name 添加菜单选项
		* Param title 名称
		* Param href 链接
		* Param iconCls 图标样式
		* Param iframe 链接跳转方式（true为iframe，false为href）
		*/	
		
		function add(id){
			addTab('评论','temp/comment.html?id='+id,null,1);
		}
		function addTab(title, href, iconCls, iframe){
			var tabPanel = parent.$('#wu-tabs');
			if(!tabPanel.tabs('exists',title)){
				var content = '<iframe scrolling="auto" frameborder="0"  src="'+ href +'" style="width:100%;height:100%;"></iframe>';
				if(iframe){
					tabPanel.tabs('add',{
						title:title,
						content:content,
						iconCls:iconCls,
						fit:true,
						cls:'pd3',
						closable:true
					});
				}
				else{
					tabPanel.tabs('add',{
						title:title,
						href:href,
						iconCls:iconCls,
						fit:true,
						cls:'pd3',
						closable:true
					});
				}
			}
			else
			{
				tabPanel.tabs('select',title);
				var current_tab=tabPanel.tabs('getSelected');
				tabPanel.tabs('update',{
					tab:current_tab,
					options:{
						content:'<iframe scrolling="auto" frameborder="0"  src="'+ href +'" style="width:100%;height:100%;"></iframe>'
					}
				})
			}
		}
		/**
		* Name 移除菜单选项
		*/
		function removeTab(){
			var tabPanel = $('#wu-tabs');
			var tab = tabPanel.tabs('getSelected');
			if (tab){
				var index = tabPanel.tabs('getTabIndex', tab);
				tabPanel.tabs('close', index);
			}
		}
    	
    	
    	//添加
    	function openNewsAdd(){
			$("#news-dialog").dialog({
				closed: false,
				modal:true,
	            title: "信息",
	            buttons: [{
	                text: '确定',
	                iconCls: 'icon-ok',
	                handler: newsAddAction,
	                
	            }, {
	                text: '取消',
	                iconCls: 'icon-cancel',
	                handler: function () {
	                    $('#news-dialog').dialog('close');                    
	                }
	            }]
	        });
		}
		
		
		function newsAddAction(){
			 $('#news-form').form('submit',{
		        onSubmit:function(){
		        	
		            if ($(this).form('enableValidation').form('validate')){
		            	console.log($('#news-form').serializeJSON());
						$.ajax({
							type:"post",
							url:"http://localhost:3000/newsgl/data",
							data:$("#news-form").serializeJSON()
						}).then(function(res){
							$('#dg').datagrid('reload');
						});
						$('#title').textbox('setValue','');
						$('#author').textbox('setValue','');
						$('#btitle').textbox('setValue','');
						$('#news-dialog').dialog('close');
		            }else{
		            	alert('no')
		            }
		        }
		    });
		}
    	
		//编辑
		function openNewsEdit(id){
			
			globalId = id;
			//alert(id);
			$.ajax({
				type:"post",
				url:"http://localhost:3000/newsgl/data/"+id,
				data:{}
			}).then(function(res){
				$('#title').textbox('setValue',res.title);
				$('#author').textbox('setValue',res.author);
				$('#btitle').textbox('setValue',res.btitle);
				//获取内容？？？？？？？？？？？？
				/*$('#news-form input[name="content"]').textbox('setValue',res.content);*/
			})
			
			$("#news-dialog").dialog({
				closed: false,
				modal:true,
	            title: "修改信息",
	            buttons: [{
	                text: '确定',
	                iconCls: 'icon-ok',
					handler: editNews,
	            }, {
	                text: '取消',
	                iconCls: 'icon-cancel',
	                handler: function () {
	                    $('#news-dialog').dialog('close');                    
	                }
	            }]
	        });
		}
		
		function editNews(){

			
			$('#news-form').form('submit',{
		        onSubmit:function(){
		            if ($(this).form('enableValidation').form('validate')){
		            	console.log($('#news-form').serializeJSON());
						$.ajax({
							type:"post",
							url:"http://localhost:3000/newsgl/data/"+globalId,
							data:$("#news-form").serializeJSON()
						}).then(function(res){
							$('#dg').datagrid('reload');
						});
						$('#title').textbox('setValue','');
						$('#author').textbox('setValue','');
						$('#btitle').textbox('setValue','');
						$('#news-dialog').dialog('close');
		            }else{
		            	alert('no')
		            }
		        }
		    });
			
			/*$('#news-dialog a.l-btn').on('click',{id:id},function(ev){
				
				if ($(this).form('enableValidation').form('validate')){
		        	console.log($('#news-form').serializeJSON());
					$.ajax({
						type:"post",
						url:"http://localhost:3000/newsgl/data/"+ev.data.id,
						data:$("#news-form").serializeJSON()
					}).then(function(res){
						console.log(res)
						
						$('#dg').datagrid('reload');
					});
					$('#news-dialog').dialog('close');
		        }else{
		        	alert('no')
		        }
			})*/
		}
		
		function deleNews(id){
			$.ajax({
				type:"delete",
				url:"http://localhost:3000/newsgl/data/"+ id
			}).then(function(res){		
				$('#dg').datagrid('reload');
			});
		}
		
	</script>
</body>
</html>
